package com.cxg.fairy.ec.main.personal.order;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.PagerAdapter;
import android.view.View;

import com.alibaba.fastjson.JSONObject;
import com.cxg.fairy.delegates.LatteDelegate;
import com.cxg.fairy.ec.detail.OrderListTabPagerAdapter;
import com.cxg.fairy.ec.main.personal.PersonalDelegate;
import com.cxg.fairy.ui.widget.TabLayout.NoScrollViewPager;
import com.cxg.fairy.util.log.LatteLogger;
import com.diabin.latte.ec.R;
import com.diabin.latte.ec.R2;


import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by CFairy
 */

public class OrderListEntryDelegate extends LatteDelegate {

    private int mType = 0;

    public static final String ORDER_TYPE = "ORDER_TYPE";

    PagerAdapter adapter = null;

    @OnClick(R2.id.back_home)
    void onClickBackHome() {
        getSupportDelegate().pop();
    }

    @BindView(R2.id.tab_layout)
    TabLayout mTabLayout = null;

    @BindView(R2.id.no_scroll_view_pager)
    NoScrollViewPager mViewPager = null;

    @Override
    public Object setLayout() {
        return R.layout.delegate_order_list_entry;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final Bundle args = getArguments();
        mType = args.getInt(PersonalDelegate.ORDER_TYPE);
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, @NonNull View rootView) {
    }

    public static OrderListEntryDelegate create(String orderType) {
        final Bundle args = new Bundle();
        args.putString(ORDER_TYPE, orderType);
        final OrderListEntryDelegate delegate = new OrderListEntryDelegate();
        delegate.setArguments(args);
        return delegate;
    }

    private void initPager() {
        JSONObject data = null;
        adapter = new OrderListTabPagerAdapter(getFragmentManager(), data,this);
        mViewPager.setOffscreenPageLimit(0);
        mViewPager.setAdapter(adapter);
    }

    private void initTabLayout() {
        mTabLayout.setTabMode(TabLayout.MODE_FIXED);
        mTabLayout.setSelectedTabIndicatorColor(ContextCompat.getColor(getContext(), R.color.app_main));
        mTabLayout.setTabTextColors(Color.BLACK,Color.RED);
        mTabLayout.setBackgroundColor(Color.WHITE);
        mTabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
        mTabLayout.setupWithViewPager(mViewPager);
    }

    @SuppressLint("NewApi")
    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);
        initTabLayout();
        initPager();
        mViewPager.setCurrentItem(mType);
        mTabLayout.getTabAt(mType).select();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
    }
}
